<?php

/*
* 案件导入
*/

class PubliccaseController extends Controller_Base
{
    public function init()
    {
        parent::init();
    }
    //案件上传批次列表
    public function indexAction()
    {
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $limit = intval($request->getQuery('limit', 10));
        $mediationOrganId  = intval($request->getQuery('mediationOrganId', 0));
        $caseStatus = intval($request->getQuery('caseStatus', 1));
        $importRecordId = intval($request->getQuery('importRecordId', ''));
        $litigantName = $request->getQuery('litigantName', '');
        $cardId = $request->getQuery('cardId', '');
        //检测批次解析和提交
        $where = $this->getWhere();
        if($mediationOrganId){
            array_push($where,"mediationOrganId = ".$mediationOrganId);
        }
        if($caseStatus){
            array_push($where,"caseStatus = ".$caseStatus);
        }
        if($importRecordId){
            array_push($where,"importRecordId = ".$importRecordId);
        }
        if($litigantName){
            array_push($where,"litigantName = '{$litigantName}'");
        }
        if($cardId){
            if(strlen((string)$cardId) == 6) {
                array_push($where, "cardId LIKE '%{$cardId}'");
            }else{
                array_push($where, "cardId = '{$cardId}'");

            }
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if($add_begin_end){
            $query['add_begin_end'] = $add_begin_end;
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "addTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "addTime >= {$begin_date}");
            }
        }
        $query = [
            'importRecordId' => $importRecordId ? $importRecordId : '',
            'litigantName' => $litigantName,
            'cardId' => $cardId,
            'mediationOrganId' => $mediationOrganId,
            'caseStatus' => $caseStatus,
            'add_begin_end' => $add_begin_end,
            'limit' => $limit,
        ];

        $page = $page ? $page : 1;
        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => ($page - 1) * $limit,
            'order' => 'id DESC'
        );

        $arrList = Module_Mediate_PublicCase::getInstance()->getAllBySearch([],$seacher,true);
        $total = Module_Mediate_PublicCase::getInstance()->getAllBySearch(["count(*) as num"], ['where' => $where], true);
        $total = $total[0]['num'];
        $totalPage = ceil($total / $limit);
        //赋值调解部门
        $this ->getMediationOrgan();

        SmartyAdapter::assign('arrList', $arrList);
        SmartyAdapter::assign('disputeType',  Module_Mediate_Case::$disputeType);
        SmartyAdapter::assign('caseStatusArr',  Module_Mediate_PublicCase::$caseStatusArr);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('page_query', http_build_query($query));
    }

    public function isshowAction(){
        $recordId = $this->getRequest()->getPost('id', 0);
        $showtype = $this->getRequest()->getPost('showtype', 0);
        $record = Module_Mediate_PublicCase::getInstance()->getItemById($recordId);
        if(empty($record)){
            AjaxTool::returnJsonData(400, '找不到相应记录');
        }
        Module_Mediate_PublicEntryImport::getInstance()->singleUpdate([
            'id' => $recordId,
            'isShow' => $showtype,
        ]);
        $status =  $showtype == 0 ? 2 : 1;
        $res = Module_Mediate_PublicCase::getInstance()->singleUpdate(['caseStatus'=>$status,'id'=>$recordId]);
        if(!$res){
            AjaxTool::returnJsonData(400, '数据库跟新异常');
        }
        $type = $showtype == 1 ?'开启':"关闭";
        if($res){
            AjaxTool::returnJsonData(200, "{$type}成功");

        }
        AjaxTool::returnJsonData(400, "{$type}失败");


    }
    //更改调解状态
    public function updateMediateStatusAction()
    {
        $request = $this->getRequest();
        $case_ids = $request->getPost('update_case_ids', '');
        $mediateStatus = $request->getPost('mediateStatus', '');
        if (empty($case_ids)) {
            AjaxTool::returnJsonData(4001);
        }
        $res = Module_Mediate_PublicCase::getInstance()->saveByIds([
            'mediateStatus' => $mediateStatus,
            'updateTime' => time()
        ], $case_ids);
        if($res){
            AjaxTool::returnJsonData(200);
        }
        AjaxTool::returnJsonData(400);

    }


}
